package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Debt;
import com.ruoyi.system.mapper.DebtMapper;
import com.ruoyi.system.service.DebtService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DebtServiceImpl extends ServiceImpl<DebtMapper, Debt> implements DebtService {
    @Autowired
    private DebtMapper debtMapper;

    /**
     * 新增欠款账单
     * @param debt
     * @return
     */
    @Override
    public int createDebt(Debt debt) {
        debt.setName(debt.getName());
        debt.setFee(debt.getFee());
        debt.setPhoneNumber(debt.getPhoneNumber());
        debt.setRemark(debt.getRemark());
        debt.setCreateTime(DateUtils.getNowDate());
        debt.setUpdateTime(DateUtils.getNowDate());
        int rows = debtMapper.insertDebt(debt);
        return rows;
    }

    /**
     * 删除欠款账单
     * @param debt
     * @return
     */
    @Override
    public int removeStatusDebt(Debt debt) {
        debt.setRemoveStatus("1");
        int rows = debtMapper.updateRemoveStatus(debt);
        return rows;
    }

    /**
     * 修改欠款账单
     * @param debt
     * @return
     */
    @Override
    public int updateDebt(Debt debt) {
//        BeanUtils.copyProperties(Debt.class,debt);
        debt.setUpdateTime(DateUtils.getNowDate());
        int rows = debtMapper.updateDebt(debt);
        return rows;
    }

    /**
     * 获取账单id
     * @param id
     * @return
     */
    @Override
    public Debt getDebtId(Integer id) {
        Debt debt = debtMapper.selectByDebtId(id);
        System.out.println(debt);
        return debt;
    }

    /**
     * 根据条件搜索账单
     * @param debt
     * @return
     */
    @Override
    public List<Debt> searchDebt(Debt debt) {
        List<Debt> debts = debtMapper.searchDebtList(debt);
        return debts;
    }

    /**
     * 账单收款
     * @param debt
     * @return
     */
    @Override
    public int payeeDebt(Debt debt) {
        debt.setStatus("0");
        debt.setReceived(debt.getFee());
        int rows = debtMapper.payeeDebtSuccess(debt);
        return rows;
    }

    /**
     * 欠款账单分页查询
     * @param debt
     * @return
     */
    @Override
    public List<Debt> selectDebtList(Debt debt) {
        List<Debt> debts = debtMapper.selectDebtList(debt);
        return debts;
    }
}
